package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.RoundScore;
import org.example.rowing.domain.Team;
import org.example.rowing.vo.RaceVO;
import org.example.rowing.vo.RegistrationVO;
import org.example.rowing.vo.RoundScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class ProcessOverFortyNineBoatsService implements OverRaceProcessStrategy {

    @Autowired
    private RaceInfoService raceInfoService;
    @Autowired
    private RaceProcessCommonService raceProcessCommonService;

    public static final List<String> teamLanes = Arrays.asList("",
            "A组3号", "A组4号", "A组2号", "A组5号", "A组1号", "A组6号",
            "B组3号", "B组4号", "B组2号", "B组5号", "B组1号", "B组6号",
            "C组3号", "C组4号", "C组2号", "C组5号", "C组1号", "C组6号",
            "D组3号", "D组4号", "D组2号", "D组5号", "D组1号", "D组6号",
            "E组3号", "E组4号", "E组2号", "E组5号", "E组1号", "E组6号",
            "F组3号", "F组4号", "F组2号", "F组5号", "F组1号", "F组6号",
            "G组3号", "G组4号", "G组2号", "G组5号", "G组1号", "G组6号",
            "H组3号", "H组4号", "H组2号", "H组5号", "H组1号", "H组6号"
    );

    /**
     * 获取计时赛晋级预赛的列表
     * @param curList
     * @param raceId
     */
    public void getTimeTrialToPre(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> preA = new ArrayList<>();
        List<RoundScoreVO> preB = new ArrayList<>();
        List<RoundScoreVO> preC = new ArrayList<>();
        List<RoundScoreVO> preD = new ArrayList<>();
        List<RoundScoreVO> preE = new ArrayList<>();
        List<RoundScoreVO> preF = new ArrayList<>();
        List<RoundScoreVO> preG = new ArrayList<>();
        List<RoundScoreVO> preH = new ArrayList<>();

        // 将预分组列表按顺序存入一个List中，方便索引访问
        List<List<RoundScoreVO>> pres = Arrays.asList(preA, preB, preC, preD, preE, preF, preG, preH);

        raceProcessCommonService.sortedEntries(curList);

        for (int i = 0; i < 48; i++) {
            int group = i / 8;   // 当前元素所在的组号
            int posInGroup = i % 8; // 在组内的位置（0-7）
            int preIndex;

            // 偶数组正序（A-H），奇数组逆序（H-A）
            if (group % 2 == 0) {
                preIndex = posInGroup;
            } else {
                preIndex = 7 - posInGroup;
            }

            // 将当前元素添加到对应的预分组列表中
            pres.get(preIndex).add(curList.get(i));
        }

        raceProcessCommonService.sortedEntries(preA);
        raceProcessCommonService.sortedEntries(preB);
        raceProcessCommonService.sortedEntries(preC);
        raceProcessCommonService.sortedEntries(preD);
        raceProcessCommonService.sortedEntries(preE);
        raceProcessCommonService.sortedEntries(preF);
        raceProcessCommonService.sortedEntries(preG);
        raceProcessCommonService.sortedEntries(preH);

        for (int i = 0; i < preA.size(); i++) {
            RoundScoreVO vo = preA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "预赛", null, teamLanes.get(i + 1), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        for (int i = 0; i < preB.size(); i++) {
            RoundScoreVO vo = preB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "预赛", null, teamLanes.get(i + 7), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        for (int i = 0; i < preC.size(); i++) {
            RoundScoreVO vo = preC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "预赛", null, teamLanes.get(i + 13), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        for (int i = 0; i < preD.size(); i++) {
            RoundScoreVO vo = preD.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "预赛", null, teamLanes.get(i + 19), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        for (int i = 0; i < preE.size(); i++) {
            RoundScoreVO vo = preE.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "预赛", null, teamLanes.get(i + 25), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        for (int i = 0; i < preF.size(); i++) {
            RoundScoreVO vo = preF.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "预赛", null, teamLanes.get(i + 31), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        for (int i = 0; i < preG.size(); i++) {
            RoundScoreVO vo = preG.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "预赛", null, teamLanes.get(i + 37), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        for (int i = 0; i < preH.size(); i++) {
            RoundScoreVO vo = preH.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "预赛", null, teamLanes.get(i + 43), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取预赛晋级半决赛的列表
     * @param curList
     */
    public void getPreToSemiFinal(List<RoundScoreVO> curList, Long raceId) {}

    /**
     * 获取预赛晋级1/4决赛的列表
     * @param curList
     */
    public void getPreToQuarterFinal(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> quarterFinalA = new ArrayList<>();
        List<RoundScoreVO> quarterFinalB = new ArrayList<>();
        List<RoundScoreVO> quarterFinalC = new ArrayList<>();
        List<RoundScoreVO> quarterFinalD = new ArrayList<>();
        List<RoundScoreVO> quarterFinalE = new ArrayList<>();
        List<RoundScoreVO> quarterFinalF = new ArrayList<>();
        List<RoundScoreVO> quarterFinalG = new ArrayList<>();
        List<RoundScoreVO> quarterFinalH = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();
        List<RoundScoreVO> groupEList = new ArrayList<>();
        List<RoundScoreVO> groupFList = new ArrayList<>();
        List<RoundScoreVO> groupGList = new ArrayList<>();
        List<RoundScoreVO> groupHList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                } else if (lane.startsWith("E组")) {
                    groupEList.add(vo);
                } else if (lane.startsWith("F组")) {
                    groupFList.add(vo);
                } else if (lane.startsWith("G组")) {
                    groupGList.add(vo);
                } else if (lane.startsWith("H组")) {
                    groupHList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);
        raceProcessCommonService.sortedEntries(groupEList);
        raceProcessCommonService.sortedEntries(groupFList);
        raceProcessCommonService.sortedEntries(groupGList);
        raceProcessCommonService.sortedEntries(groupHList);

        allocateByRank(groupAList, quarterFinalA, quarterFinalD, quarterFinalC, 1, 3);
        allocateByRank(groupBList, quarterFinalB, quarterFinalC, quarterFinalD, 1, 3);
        allocateByRank(groupCList, quarterFinalC, quarterFinalB, quarterFinalA, 1, 3);
        allocateByRank(groupDList, quarterFinalD, quarterFinalA, quarterFinalB, 1, 3);
        allocateByRank(groupEList, quarterFinalD, quarterFinalA, quarterFinalB, 1, 3);
        allocateByRank(groupFList, quarterFinalC, quarterFinalB, quarterFinalA, 1, 3);
        allocateByRank(groupGList, quarterFinalB, quarterFinalC, quarterFinalD, 1, 3);
        allocateByRank(groupHList, quarterFinalA, quarterFinalD, quarterFinalC, 1, 3);

        allocateByRank(groupAList, quarterFinalE, quarterFinalH, quarterFinalG, 4, 6);
        allocateByRank(groupBList, quarterFinalF, quarterFinalG, quarterFinalH, 4, 6);
        allocateByRank(groupCList, quarterFinalG, quarterFinalF, quarterFinalE, 4, 6);
        allocateByRank(groupDList, quarterFinalH, quarterFinalE, quarterFinalF, 4, 6);
        allocateByRank(groupEList, quarterFinalH, quarterFinalF, quarterFinalE, 4, 6);
        allocateByRank(groupFList, quarterFinalG, quarterFinalF, quarterFinalE, 4, 6);
        allocateByRank(groupGList, quarterFinalF, quarterFinalG, quarterFinalH, 4, 6);
        allocateByRank(groupHList, quarterFinalE, quarterFinalH, quarterFinalG, 4, 6);


        raceProcessCommonService.sortedEntries(quarterFinalA);
        raceProcessCommonService.sortedEntries(quarterFinalB);
        raceProcessCommonService.sortedEntries(quarterFinalC);
        raceProcessCommonService.sortedEntries(quarterFinalD);
        raceProcessCommonService.sortedEntries(quarterFinalE);
        raceProcessCommonService.sortedEntries(quarterFinalF);
        raceProcessCommonService.sortedEntries(quarterFinalG);
        raceProcessCommonService.sortedEntries(quarterFinalH);

        for (int i = 0; i < quarterFinalA.size(); i++) {
            RoundScoreVO vo = quarterFinalA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 1), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalB.size(); i++) {
            RoundScoreVO vo = quarterFinalB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 7), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalC.size(); i++) {
            RoundScoreVO vo = quarterFinalC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 13), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalD.size(); i++) {
            RoundScoreVO vo = quarterFinalD.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 19), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalE.size(); i++) {
            RoundScoreVO vo = quarterFinalE.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 25), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalF.size(); i++) {
            RoundScoreVO vo = quarterFinalF.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 31), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalG.size(); i++) {
            RoundScoreVO vo = quarterFinalG.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 37), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalH.size(); i++) {
            RoundScoreVO vo = quarterFinalH.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 43), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取预赛晋级复赛的列表
     * @param curList
     * @param raceId
     */
    public void getPreToIntermediary(List<RoundScoreVO> curList, Long raceId) {
    }

    /**
     * 获取复赛晋级半决赛的列表
     * @param curList
     * @param raceId
     */
    public void getInterToSemiFinal(List<RoundScoreVO> curList, Long raceId) {
    }

    /**
     * 获取复赛晋级1/4决赛的列表
     * @param curList
     * @param raceId
     */
    public void getInterToQuarterFinal(List<RoundScoreVO> curList, Long raceId) {
    }

    /**
     * 获取复赛晋级决赛的列表
     * @param curList
     * @param raceId
     */
    public void getInterToFinal(List<RoundScoreVO> curList, Long raceId) {}

    /**
     * 获取1/4决赛晋级半决赛的列表
     * @param curList
     * @param raceId
     */
    public void getQuarterToSemiFinal(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> semiFinalA = new ArrayList<>();
        List<RoundScoreVO> semiFinalB = new ArrayList<>();
        List<RoundScoreVO> semiFinalC = new ArrayList<>();
        List<RoundScoreVO> semiFinalD = new ArrayList<>();
        List<RoundScoreVO> semiFinalE = new ArrayList<>();
        List<RoundScoreVO> semiFinalF = new ArrayList<>();
        List<RoundScoreVO> semiFinalG = new ArrayList<>();
        List<RoundScoreVO> semiFinalH = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();
        List<RoundScoreVO> groupEList = new ArrayList<>();
        List<RoundScoreVO> groupFList = new ArrayList<>();
        List<RoundScoreVO> groupGList = new ArrayList<>();
        List<RoundScoreVO> groupHList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                } else if (lane.startsWith("E组")) {
                    groupEList.add(vo);
                } else if (lane.startsWith("F组")) {
                    groupFList.add(vo);
                } else if (lane.startsWith("G组")) {
                    groupGList.add(vo);
                } else if (lane.startsWith("H组")) {
                    groupHList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);
        raceProcessCommonService.sortedEntries(groupEList);
        raceProcessCommonService.sortedEntries(groupFList);
        raceProcessCommonService.sortedEntries(groupGList);
        raceProcessCommonService.sortedEntries(groupHList);

        allocateByRank(groupAList, semiFinalA, semiFinalB, 1, 3);
        allocateByRank(groupBList, semiFinalA, semiFinalB, 1, 3);
        allocateByRank(groupCList, semiFinalB, semiFinalA, 1, 3);
        allocateByRank(groupDList, semiFinalB, semiFinalA, 1, 3);

        allocateByRank(groupAList, semiFinalD, semiFinalC, 4, 6);
        allocateByRank(groupBList, semiFinalD, semiFinalC, 4, 6);
        allocateByRank(groupCList, semiFinalC, semiFinalD, 4, 6);
        allocateByRank(groupDList, semiFinalC, semiFinalD, 4, 6);

        allocateByRank(groupEList, semiFinalE, semiFinalF, 1, 3);
        allocateByRank(groupFList, semiFinalE, semiFinalF, 1, 3);
        allocateByRank(groupGList, semiFinalF, semiFinalE, 1, 3);
        allocateByRank(groupHList, semiFinalF, semiFinalE, 1, 3);

        allocateByRank(groupEList, semiFinalH, semiFinalG, 4, 6);
        allocateByRank(groupFList, semiFinalH, semiFinalG, 4, 6);
        allocateByRank(groupGList, semiFinalG, semiFinalH, 4, 6);
        allocateByRank(groupHList, semiFinalG, semiFinalH, 4, 6);

        raceProcessCommonService.sortedEntries(semiFinalA);
        raceProcessCommonService.sortedEntries(semiFinalB);
        raceProcessCommonService.sortedEntries(semiFinalC);
        raceProcessCommonService.sortedEntries(semiFinalD);
        raceProcessCommonService.sortedEntries(semiFinalE);
        raceProcessCommonService.sortedEntries(semiFinalF);
        raceProcessCommonService.sortedEntries(semiFinalG);
        raceProcessCommonService.sortedEntries(semiFinalH);

        for (int i = 0; i < semiFinalA.size(); i++) {
            RoundScoreVO vo = semiFinalA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 1), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalB.size(); i++) {
            RoundScoreVO vo = semiFinalB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 7), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalC.size(); i++) {
            RoundScoreVO vo = semiFinalC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 13), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalD.size(); i++) {
            RoundScoreVO vo = semiFinalD.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 19), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalE.size(); i++) {
            RoundScoreVO vo = semiFinalE.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 25), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalF.size(); i++) {
            RoundScoreVO vo = semiFinalF.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 31), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalG.size(); i++) {
            RoundScoreVO vo = semiFinalG.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 37), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalH.size(); i++) {
            RoundScoreVO vo = semiFinalH.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 43), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取半决赛晋级决赛的列表
     * @param curList
     * @param raceId
     */
    public void getSemiToFinal(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> finalA = new ArrayList<>();
        List<RoundScoreVO> finalB = new ArrayList<>();
        List<RoundScoreVO> finalC = new ArrayList<>();
        List<RoundScoreVO> finalD = new ArrayList<>();
        List<RoundScoreVO> finalE = new ArrayList<>();
        List<RoundScoreVO> finalF = new ArrayList<>();
        List<RoundScoreVO> finalG = new ArrayList<>();
        List<RoundScoreVO> finalH = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();
        List<RoundScoreVO> groupEList = new ArrayList<>();
        List<RoundScoreVO> groupFList = new ArrayList<>();
        List<RoundScoreVO> groupGList = new ArrayList<>();
        List<RoundScoreVO> groupHList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                } else if (lane.startsWith("E组")) {
                    groupEList.add(vo);
                } else if (lane.startsWith("F组")) {
                    groupFList.add(vo);
                } else if (lane.startsWith("G组")) {
                    groupGList.add(vo);
                } else if (lane.startsWith("H组")) {
                    groupHList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);
        raceProcessCommonService.sortedEntries(groupEList);
        raceProcessCommonService.sortedEntries(groupFList);
        raceProcessCommonService.sortedEntries(groupGList);
        raceProcessCommonService.sortedEntries(groupHList);

        for (int i = 0; i < 3; i++) {
            if (i % 2 == 0) {
                finalA.add(groupAList.get(i));
                finalA.add(groupBList.get(i));
            } else {
                finalA.add(groupBList.get(i));
                finalA.add(groupAList.get(i));
            }
        }
        for (int i = 3; i < 6; i++) {
            if (i % 2 == 0) {
                finalB.add(groupAList.get(i));
                finalB.add(groupBList.get(i));
            } else {
                finalB.add(groupBList.get(i));
                finalB.add(groupAList.get(i));
            }
        }
        for (int i = 0; i < 3; i++) {
            if (i % 2 == 0) {
                finalC.add(groupCList.get(i));
                finalC.add(groupDList.get(i));
            } else {
                finalC.add(groupDList.get(i));
                finalC.add(groupCList.get(i));
            }
        }
        for (int i = 3; i < 6; i++) {
            if (i % 2 == 0) {
                finalD.add(groupCList.get(i));
                finalD.add(groupDList.get(i));
            } else {
                finalD.add(groupDList.get(i));
                finalD.add(groupCList.get(i));
            }
        }
        for (int i = 0; i < 3; i++) {
            if (i % 2 == 0) {
                finalE.add(groupEList.get(i));
                finalE.add(groupFList.get(i));
            } else {
                finalE.add(groupFList.get(i));
                finalE.add(groupEList.get(i));
            }
        }
        for (int i = 3; i < 6; i++) {
            if (i % 2 == 0) {
                finalF.add(groupEList.get(i));
                finalF.add(groupFList.get(i));
            } else {
                finalF.add(groupFList.get(i));
                finalF.add(groupEList.get(i));
            }
        }
        for (int i = 0; i < 3; i++) {
            if (i % 2 == 0) {
                finalG.add(groupGList.get(i));
                finalG.add(groupHList.get(i));
            } else {
                finalG.add(groupHList.get(i));
                finalG.add(groupGList.get(i));
            }
        }
        for (int i = 3; i < 6; i++) {
            if (i % 2 == 0) {
                finalH.add(groupGList.get(i));
                finalH.add(groupHList.get(i));
            } else {
                finalH.add(groupHList.get(i));
                finalH.add(groupGList.get(i));
            }
        }

        for (int i = 0; i < finalA.size(); i++) {
            RoundScoreVO vo = finalA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 1), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalB.size(); i++) {
            RoundScoreVO vo = finalB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 7), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalC.size(); i++) {
            RoundScoreVO vo = finalC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 13), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalD.size(); i++) {
            RoundScoreVO vo = finalD.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 19), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalE.size(); i++) {
            RoundScoreVO vo = finalE.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 25), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalF.size(); i++) {
            RoundScoreVO vo = finalF.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 31), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalG.size(); i++) {
            RoundScoreVO vo = finalG.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 37), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalH.size(); i++) {
            RoundScoreVO vo = finalH.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 43), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取最终排名
     * @param curList
     * @param raceVO
     * @return
     */
    public List<RoundScoreVO> getRank(List<RoundScoreVO> curList, RaceVO raceVO) {
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();
        List<RoundScoreVO> groupEList = new ArrayList<>();
        List<RoundScoreVO> groupFList = new ArrayList<>();
        List<RoundScoreVO> groupGList = new ArrayList<>();
        List<RoundScoreVO> groupHList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                } else if (lane.startsWith("E组")) {
                    groupEList.add(vo);
                } else if (lane.startsWith("F组")) {
                    groupFList.add(vo);
                } else if (lane.startsWith("G组")) {
                    groupGList.add(vo);
                } else if (lane.startsWith("H组")) {
                    groupHList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);
        raceProcessCommonService.sortedEntries(groupEList);
        raceProcessCommonService.sortedEntries(groupFList);
        raceProcessCommonService.sortedEntries(groupGList);
        raceProcessCommonService.sortedEntries(groupHList);

        groupAList.addAll(groupBList);
        groupAList.addAll(groupCList);
        groupAList.addAll(groupDList);
        groupAList.addAll(groupEList);
        groupAList.addAll(groupFList);
        groupAList.addAll(groupGList);
        groupAList.addAll(groupHList);

        saveFinalResult(groupAList, raceVO);

        int i = 0;
        for (RoundScoreVO roundScoreVO : groupAList) {
            Double v = Double.valueOf(i++);
            RoundScore roundScore = new RoundScore(raceVO.getId(), "比赛结果", null, null, v, roundScoreVO.getRegistrationVO().getRegistrationId(), roundScoreVO.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        return groupAList;
    }


    /**
     *
     * @param group
     * @param halfA
     * @param halfB
     * @param start 起始排名（从1开始）
     * @param end   结束排名
     */
    private void allocateByRank(List<RoundScoreVO> group, List<RoundScoreVO> halfA, List<RoundScoreVO> halfB, int start, int end) {
        int startIndex = start - 1; // 第1名（索引0）
        int endIndex = Math.min(group.size(), end); // 最多取到第5名（索引5）

        for (int i = startIndex; i < endIndex; i++) {
            if (i % 2 == 0) {
                halfA.add(group.get(i)); // 2 4
            } else {
                halfB.add(group.get(i)); // 3
            }
        }
    }

    private void allocateByRank(List<RoundScoreVO> group, List<RoundScoreVO> halfA, List<RoundScoreVO> halfB, List<RoundScoreVO> halfC, int start, int end) {
        int startIndex = start - 1; // 第1名（索引0）
        int endIndex = Math.min(group.size(), end); // 最多取到第5名（索引5）

        for (int i = startIndex; i < endIndex; i++) {
            if (i % 3 == 0) {
                halfA.add(group.get(i)); // 0
            } else if (i % 3 == 1) {
                halfB.add(group.get(i)); // 1
            } else if (i % 3 == 2) {
                halfC.add(group.get(i)); // 2
            }
        }
    }

    private void allocateByRank(List<RoundScoreVO> group, List<RoundScoreVO> halfA, List<RoundScoreVO> halfB, List<RoundScoreVO> halfC, List<RoundScoreVO> halfD, int start, int end) {
        int startIndex = start - 1; // 第1名（索引0）
        int endIndex = Math.min(group.size(), end); // 最多取到第5名（索引5）

        for (int i = startIndex; i < endIndex; i++) {
            if (i % 4 == 0) {
                halfA.add(group.get(i)); // 4
            } else if (i % 4 == 1) {
                halfB.add(group.get(i)); // 1 5
            } else if (i % 4 == 2) {
                halfC.add(group.get(i)); // 2
            } else if (i % 4 == 3) {
                halfD.add(group.get(i)); // 3
            }
        }
    }

    private void saveFinalResult(List<RoundScoreVO> finalList, RaceVO raceVO) {
        raceProcessCommonService.saveFinalResult(finalList, raceVO);
    }
}
